package com.example.guethe.fireanalysisapp.fragments.user;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.guethe.fireanalysisapp.R;
import com.example.guethe.fireanalysisapp.activities.HomeActivity;
import com.example.guethe.fireanalysisapp.data.beans.User;
import com.example.guethe.fireanalysisapp.present.user.LoginPresenter;
import com.example.guethe.fireanalysisapp.util.LoginHelper;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.mvp.XLazyFragment;
import cn.droidlover.xdroidmvp.router.Router;

import static com.example.guethe.fireanalysisapp.util.BaseUtil.isFastClick;

/**
 * Created by 何建钦 on 2018/3/19.
 */

public class LoginFragment extends XLazyFragment<LoginPresenter> {

    @BindView(R.id.et_username)
    public EditText et_username;
    @BindView(R.id.et_password)
    public EditText et_password;

    @BindView(R.id.bt_username_clear)
    public Button bt_username_clear;
    @BindView(R.id.bt_pwd_clear)
    public Button bt_pwd_clear;
    @BindView(R.id.bt_login)
    public Button bt_login;

    public TextWatcher username_watcher;
    public TextWatcher password_watcher;

    private View rootView;




    @Override
    public void initData(Bundle savedInstanceState) {

        // 获取焦点
        et_username.setText(LoginHelper.getUser().name);
        et_username.requestFocus();
        initWatcher();
        et_username.addTextChangedListener(username_watcher);
        et_password.addTextChangedListener(password_watcher);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_login;
    }

    @Override
    public LoginPresenter newP() {
        return new LoginPresenter();
    }



    @OnClick(R.id.bt_username_clear)
    public void clearEtUsername(){
        et_username.setText("");
        et_password.setText("");
    }


    @OnClick(R.id.bt_pwd_clear)
    public void clearEtPassword(){
        et_password.setText("");
    }

    @OnClick(R.id.bt_login)
    public void login (){
        if (isFastClick()) {
            getP().login();
        }
    }

    public void toHomeActivity () {
        Router.newIntent(getActivity())
                .to(HomeActivity.class)
                .launch();
    }


    private void initWatcher() {
        username_watcher = new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
            @Override
            public void afterTextChanged(Editable s) {
                et_password.setText("");
                if(s.toString().length()>0){
                    bt_username_clear.setVisibility(View.VISIBLE);
                }else{
                    bt_username_clear.setVisibility(View.INVISIBLE);
                }
            }
        };
        password_watcher = new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
            @Override
            public void afterTextChanged(Editable s) {
                if(s.toString().length()>0){
                    bt_pwd_clear.setVisibility(View.VISIBLE);
                }else{
                    bt_pwd_clear.setVisibility(View.INVISIBLE);
                }
            }
        };
    }

}
